// 全局变量以g_开头
// 私有函数以_开头

import { Config } from 'config.js';

class Token {
    constructor() {
      this.loginUrl = Config.restUrl + '/wechat/applet/user/login';
      this.registerUrl = Config.restUrl + '/wechat/applet/user/register';
      this.transferUrl = Config.restUrl + '/wechat/applet/user/transfer';
    }
    
    getTokenFromServer(userData, callBack) {
        var that  = this;
        wx.request({
          url: that.loginUrl,
            method:'POST',
            data: {
              ...userData,
              appId: Config.appId
            },
            success:function(res){
              const result = res.data
              result.data = result.data || {}
              if (result.status === 400 && result.errorCode == '10002') {
                wx.reLaunch({
                  url: '/pages/login/index'
                })
              } else {
                wx.setStorageSync('token', result.data.token);
                wx.setStorageSync('merchantId', result.data.merchantId);
                callBack && callBack(result);
              }
            }
        })
    }
}

export {Token};